package cn.gogoyc.org.controller;

import cn.gogoyc.org.basic.PreAuthorize;
import cn.gogoyc.org.basic.util.AjaxResult;
import cn.gogoyc.org.basic.util.PageList;
import cn.gogoyc.org.domain.Department;
import cn.gogoyc.org.domain.Employee;
import cn.gogoyc.org.query.EmployeeQuery;
import cn.gogoyc.org.service.DepartmentService;
import cn.gogoyc.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * The type Department controller.
 * 员工接口
 *
 * @author Yang
 * @date 2023 /1/12 10:04
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工接口类", description = "处理员工的CRUD操作")
public class EmployeeController {

    @Autowired
    private EmployeeService service;

    @Autowired
    private DepartmentService service2;

    /**
     * Find all list.
     * 查询单个员工对象
     *
     * @return the list
     */
    @GetMapping
    @ApiOperation(value = "接口:查询所有对象")
    @PreAuthorize(name = "查询所有部门",sn = "employee:getAll")
    public List<Employee> findAll() {
        return service.loadAll();
    }

    /**
     * Query page page list.
     *
     * @param query the query
     * @return the page list
     */
    @PostMapping
    @ApiOperation(value = "接口:查询分页对象")
    @PreAuthorize(name = "分页列表权限",sn = "employee:list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return service.queryPage(query);
    }


    /**
     * Delete ajax result.
     *
     * @param id the id
     * @return the ajax result
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "接口:删除单个对象")
    @PreAuthorize(name = "员工删除",sn = "employee:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败",false);
        }
    }

    /**
     * Add or update ajax result.
     *
     * @param employee the employee
     * @return the ajax result
     */
    @PutMapping
    @ApiOperation(value = "接口:添加单个对象或修改单个对象")
    @PreAuthorize(name = "员工添加或删除",sn = "employee:updateAndSave")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                service.save(employee);
                return new AjaxResult("操作成功",true);
            }else {
                service.update(employee);
                return new AjaxResult("操作成功",true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败",false);
        }
    }

    /**
     * Patch delete ajax result.
     *
     * @param ids the ids
     * @return the ajax result
     */
    @PatchMapping
    @PreAuthorize(name = "员工批量删除",sn = "employee:patchDel")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            service.patchDelete(ids);
            return new AjaxResult("操作成功",true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败",false);
        }
    }

    @ApiOperation(value = "接口：查询部门树")
    @GetMapping("/deptTree")
    @PreAuthorize(name = "部门无限极树",sn = "employee:getTree")
    public List<Department> deptTree(){
        return service2.deptTree();
    }


    @GetMapping("/list/{shopId}")
    public List<Employee> queryByShopId(@PathVariable("shopId") Long shopId){
        return service.queryByShopId(shopId);
    }
}
